Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

i18n support for file collections - closes #4483 #4487

Closed
wants to merge 31 commits into from

Conversation

gewfy
Copy link

@gewfy gewfy commented Oct 21, 2020

Summary
This adds i18n support for file collections as per #4483

Test plan
Fixed unit tests:

  • Removed config > applyDefaults > i18n > should throw when i18n is set on files collection
  • Added config > applyDefaults > i18n > should set i18n value to translate on field when i18n=true for field in files collection

Working in development with i18n: true on the Settings > Site Settings file collection.
image

A picture of a cute animal (not mandatory but encouraged)
Worlds smallest horse!
image

@gewfy gewfy requested a review from a team October 21, 2020 12:38
@gewfy gewfy changed the title WIP: i18n support for file collections - closes #4483 i18n support for file collections - closes #4483 Oct 21, 2020
@erezrokah erezrokah added the type: feature code contributing to the implementation of a feature and/or user facing functionality label Oct 21, 2020
Copy link
Contributor

@erezrokah erezrokah left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @gewfy.
This enables the translation UI for file collections but doesn't handle persisting the content.

Can you please share an example configuration and the resulting content files of saving a file with i18n support? How would those files be named and structured in the repo?

@gewfy
Copy link
Author

gewfy commented Oct 26, 2020

@erezrokah Ok so if I understand you correctly. Basically having a file collection configured without the actual file being there? I see that this errors now. Will see if I can fix that.

We're thinking about adding i18n Posts and i18n Settings collections to the dev-test.html. Is that a good idea you think?

@gewfy
Copy link
Author

gewfy commented Oct 27, 2020

@erezrokah I've limited this change to only support single_file structures since (I'm guessing) that adding support for multiple_folders and multiple_files would require a different way of configuring files collections.

What I've done:

  • Made sure that single_file files collections is editable and persists for both existing and non-existing files
  • Throw if the top level or collection level i18n structure is set to anything else than single_file
  • Tweaked the i18n limitations parts of the docs
  • Added i18n Posts and i18n Settings to dev-test.html for easier testing

Good enough trade-off?

@gewfy gewfy requested a review from erezrokah October 27, 2020 20:05
Copy link
Contributor

@erezrokah erezrokah left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @gewfy, enabling single_file for file collections makes sense.
Added a few comments.
I think the changes will require splitting https://github.com/netlify/netlify-cms/blob/519cb2d4c2db729d2643c9116f93656b6a9dba23/packages/netlify-cms-core/src/actions/config.js#L71

  1. Set the defaults for the collection based on the top level config.
  2. Set the defaults for each file based on the collection config.
  3. Set the defaults for each field based on the collection/file.

dev-test/config.yml Show resolved Hide resolved
@@ -634,7 +634,7 @@ describe('config', () => {
).toEqual({ structure: 'multiple_folders', locales: ['en', 'fr'], default_locale: 'fr' });
});

it('should throw when i18n is set on files collection', () => {
it('should throw when i18n structure is not single_file on files collection', () => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we explicitly test for an error being thrown when structure is one of multiple_folders, multiple_files?

}

if (collection.has('files')) {
collection = collection.set('files', traverseFields(collection.get('files'), setI18nField));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

traverseFields fields expects a list of fields and not of files.

if (collection.has('files')) {
collection = collection.set(
'files',
traverseFields(collection.get('files'), field => field.delete(I18N)),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

traverseFields fields expects a list of fields and not of files.

@@ -231,9 +250,17 @@ export function applyDefaults(config) {

const files = collection.get('files');
if (files) {
if (i18n && collection.has(I18N)) {
throw new Error('i18n configuration is not supported for files collection');
if (
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we extract this logic to a function, e.g. isI18nAllowed?
Also I think setting:

file:
  i18n:
    structure: multiple_folders

Would pass this check

@@ -245,9 +272,11 @@ export function applyDefaults(config) {
'fields',
traverseFields(file.get('fields'), setDefaultPublicFolder),
);
file = setI18nDefaults(i18n, file);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

files should inherit collection level configuration. I think calling setI18nDefaults this way will inherit top level configuration.

This will require re-using some of the logic from here:
https://github.com/netlify/netlify-cms/blob/519cb2d4c2db729d2643c9116f93656b6a9dba23/packages/netlify-cms-core/src/actions/config.js#L74

@erezrokah
Copy link
Contributor

Replaced by #4634 as I couldn't push to the branch of this PR

@erezrokah erezrokah closed this Nov 25, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: feature code contributing to the implementation of a feature and/or user facing functionality
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants